package com.hzkq.widgets.chart.simple.pseudo3d;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;

import com.hzkq.widgets.R;
import com.keqiang.chart.impl.pie.IndicatorLine;
import com.keqiang.chart.impl.pie.LabelPosition;
import com.keqiang.chart.impl.pie.impl.normal.PieEntity;
import com.keqiang.chart.impl.pie.impl.pseudo3d.PieChart3D;
import com.keqiang.chart.impl.pie.impl.pseudo3d.PieChart3DHighLightRender;
import com.keqiang.chart.impl.pie.impl.pseudo3d.PieChart3DView;
import com.keqiang.chart.utils.UtilsKt;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;
import me.zhouzhuo810.magpiex.ui.widget.TitleBar;
import me.zhouzhuo810.magpiex.utils.SimpleUtil;

/**
 * @author Created by wanggaowan on 2022/7/26 15:00
 */
public class PieChartActivity extends BaseActivity {
    
    private TitleBar mTitleBar;
    private PieChart3D mPieChart;
    private PieChart3D mPieChart2;
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_pie_chart_2_5d;
    }
    
    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }
    
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.title_bar);
        PieChart3DView pieChartView = findViewById(R.id.pie_chart);
        mPieChart = pieChartView.getChart();
        PieChart3DView pieChartView2 = findViewById(R.id.pie_chart2);
        mPieChart2 = pieChartView2.getChart();
        initPieChart();
        initPieChart2();
    }
    
    @Override
    public void initData() {
        fillPieChartData();
        fillPieChartData2();
    }
    
    @Override
    public void initEvent() {
        mTitleBar.getLlLeft().setOnClickListener(v -> closeAct());
    }
    
    private void initPieChart() {
        mPieChart.setRadius(SimpleUtil.getScaledValue(300));
        mPieChart.setLabelPosition(LabelPosition.inside());
        mPieChart.setHeight(SimpleUtil.getScaledValue(100f));
        
        PieChart3DHighLightRender render = new PieChart3DHighLightRender(mPieChart);
        render.setHighLightColor(pieEntity -> UtilsKt.setColorLuminance(pieEntity.getColor(), 0.7f));
        render.setOffsetRadius(0f);
        mPieChart.setHighLightRender(render);
        
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setPathEffect(new DashPathEffect(new float[]{10f, 10f}, 0));
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(SimpleUtil.getScaledValue(6));
    }
    
    private void initPieChart2() {
        mPieChart2.setRadius(SimpleUtil.getScaledValue(300));
        mPieChart2.setLabelPosition(LabelPosition.outside()
            .lineFactory(entity -> IndicatorLine.newInstance()
                .color(entity.getColor())
                .offset(SimpleUtil.getScaledValue(6f))
                .drawStartCircle(true)
                .circleColor(entity.getColor())));
        
        mPieChart2.setHeight(SimpleUtil.getScaledValue(100f));
        mPieChart2.setInnerRadius(SimpleUtil.getScaledValue(200));
        mPieChart2.setSliceSpace(SimpleUtil.getScaledValue(10f));
        
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setPathEffect(new DashPathEffect(new float[]{10f, 10f}, 0));
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(SimpleUtil.getScaledValue(6));
    }
    
    /**
     * 设置图表数据
     */
    private void fillPieChartData() {
        mPieChart.setDrawStartAngle(-45f);
        List<PieEntity> data = new ArrayList<>();
        for (int j = 0; j < 4; j++) {
            PieEntity entity = new PieEntity((float) (Math.random() * 200f));
            entity.setDrawLabel(true);
            entity.setLabel(((int) entity.getValue()) + "");
            entity.setLabelSize(SimpleUtil.getScaledValue(42));
            entity.setLabelColor(Color.WHITE);
            entity.setSecondLabelSize(SimpleUtil.getScaledValue(30));
            entity.setSecondLabelColor(Color.GRAY);
            
            if (j % 4 == 0) {
                entity.setColor(Color.GREEN);
                entity.setSecondLabel("手游");
            } else if (j % 4 == 1) {
                entity.setColor(Color.DKGRAY);
                entity.setSecondLabel("主机");
            } else if (j % 4 == 2) {
                entity.setColor(Color.RED);
                entity.setSecondLabel("PC");
            } else {
                entity.setColor(Color.BLUE);
                entity.setSecondLabel("其它");
            }
            data.add(entity);
        }
        mPieChart.setData(data);
        mPieChart.animDraw(2000);
    }
    
    /**
     * 设置图表数据
     */
    private void fillPieChartData2() {
        mPieChart2.setDrawStartAngle(-45f);
        List<PieEntity> data = new ArrayList<>();
        for (int j = 0; j < 4; j++) {
            PieEntity entity = new PieEntity((float) (Math.random() * 200f));
            entity.setDrawLabel(true);
            entity.setLabel(((int) entity.getValue()) + "");
            entity.setLabelSize(SimpleUtil.getScaledValue(42));
            entity.setSecondLabelSize(SimpleUtil.getScaledValue(30));
            entity.setSecondLabelColor(Color.GRAY);
            
            if (j % 4 == 0) {
                entity.setColor(Color.GREEN);
                entity.setSecondLabel("手游");
                // entity.setAddedRadius(SimpleUtil.getScaledValue(40));
            } else if (j % 4 == 1) {
                entity.setColor(Color.DKGRAY);
                entity.setSecondLabel("主机");
            } else if (j % 4 == 2) {
                entity.setColor(Color.RED);
                entity.setSecondLabel("PC");
            } else {
                entity.setColor(Color.BLUE);
                entity.setSecondLabel("其它");
            }
            data.add(entity);
        }
        mPieChart2.setData(data);
        mPieChart2.animDraw(2000);
    }
}
